package com.example.testcomposeproject.pages.sideEffect.testSideEffect

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.navigation.compose.hiltViewModel
import com.example.testcomposeproject.MyLogger
import com.example.testcomposeproject.PreViewData
import com.example.testcomposeproject.pages.copy.CopyViewModel
import com.example.testcomposeproject.ui.theme.AppTheme

/**
 * @author: hemin
 * @date: 2024/6/21
 * @Desc: 这个思路可以包装高德地图的MapView
 *
 */
@Preview(showBackground = true, device = PreViewData.PreviewPhoneSize)
@Composable
fun PreViewTestSideEffectUI(modifier: Modifier = Modifier) {
    AppTheme {
        TestSideEffectUI(modifier = modifier)

    }
}

/**
 * viewModel 用这个方法
 */
@Composable
fun TestSideEffectScreen(
    modifier: Modifier = Modifier,
    viewModel: CopyViewModel = hiltViewModel()
) {
    TestSideEffectUI(modifier)
}

/**
 * 真正的UI元素
 */
@Composable
fun TestSideEffectUI(modifier: Modifier = Modifier) {
    var show by remember {
        mutableStateOf(false)
    }

   SideEffect {
       MyLogger.hLog().e("SideEffect监听show的变化:$show")
   }
    Column(
        modifier = Modifier.fillMaxSize(),
        horizontalAlignment = androidx.compose.ui.Alignment.CenterHorizontally,
        verticalArrangement = androidx.compose.foundation.layout.Arrangement.Center
    ) {
        Button(onClick = { show = !show }) {
            Text(text = "点击")
        }

        Text(text = if (show) "显示" else "隐藏")
    }

}


